// 辅助时间框选相关方法

const other = [{ title: '00:00 - 12:00', children: [] }, { title: '12:00 - 24:00', children: [] }]
for (let i = 0; i < 12; i++) {
  other[0].children.push({
    title: i < 10 ? '0' + i : i,
    children: [
      { dataIndex: `t${i}a`, key: `t${i}a`, title: ''  },
      { dataIndex: `t${i}b`, key: `t${i}b`, title: ''  }
    ]
  })
  other[1].children.push({
    title: i + 12,
    children: [
      { dataIndex: `t${i + 12}a`, key: `t${i + 12}a`, title: '' },
      { dataIndex: `t${i + 12}b`, key: `t${i + 12}b`, title: '' }
    ]
  })
}
const columns = [{ title: '星期/时间', dataIndex: 'week', key: 'week', width: '100px', align: 'center' }, ...other]
let data = [{ week: '星期一' }, { week: '星期二' }, { week: '星期三' }, { week: '星期四' }, { week: '星期五' }, { week: '星期六' }, { week: '星期日' }];
const times = {}
const timesNoData = {}
for (let i = 0; i < 24; i++) {
  for (let j = 0; j < 2; j++) {
    times['t' + i + (j === 0 ? 'a' : 'b')] = i + '' + (j === 0 ? '-0' : '-1')
    timesNoData['t' + i + (j === 0 ? 'a' : 'b')] = ''
  }
}

data = data.map(item => ({ ...item, ...times }))
const dataSouce = data.map((item, index) => ({ ...item, ...timesNoData, index }))
const dataWithIndex = data.map((item, i) => {
  const newObj = {}
  for (const key in item) {
    if (Object.hasOwnProperty.call(item, key)) {
      newObj[key] = item[key].indexOf('星期') === -1 ? item[key] + '-' + i : item[key]
    }
  }
  return { ...newObj }
})

// 将起始坐标和终点坐标替换为左上角到右下角
function pointSort(pA, pB) {
  let res = [[pA[0], pA[1]], [pB[0], pB[1]]]
  if (pA[0] > pB[0]) {
    res = [[res[1][0], res[0][1]], [res[0][0], res[1][1]]]
  }
  if (pA[1] > pB[1]) {
    res = [[res[0][0], res[1][1]], [res[1][0], res[0][1]]]
  }
  return res
}

// 修改dom的style
function changeDomStyle(styles, dom) {
  const keys = Object.keys(styles)
  let string = ''
  keys.forEach(name => {
    string += `${name}:${styles[name]};`
  })
  dom.style = string
}

export { dataWithIndex, dataSouce, columns, pointSort, changeDomStyle }
